/*
    This file is part of SisIB - Sistema de Identificação Biométrica.

    Copyright (C) 2014  Thiago Azevedo Alves.

    SisIB is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    SisIB is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with SisIB.  If not, see <http://www.gnu.org/licenses/>.
 */
package sisib;

import java.io.File;
import java.util.Locale;
import javax.imageio.spi.ImageReaderWriterSpi;
import javax.swing.filechooser.FileFilter;

/**
 *
 * @author Thiago
 */
public class ImageFileFilter extends FileFilter{
    private ImageReaderWriterSpi spi;
    private String[] suffixes;
    
    public ImageFileFilter(ImageReaderWriterSpi spi){
        this.spi = spi;
        suffixes = spi.getFileSuffixes();
    }

    public boolean accept(File f) {
        if (f.isDirectory()) return true;
        String fileName = f.getName();
        int i = fileName.lastIndexOf('.');
        String suffix = fileName.substring(i + 1);
        for (i = 0; i < suffixes.length; i++)
            if (suffix.equalsIgnoreCase(suffixes[i]))
                return true;
        return false;

    }

    public String getDescription(){
        StringBuffer buffer = new StringBuffer();
        int i;
        buffer.append(spi.getDescription(Locale.getDefault()));
        buffer.append(" (");
        for (i = 0; i < suffixes.length; i++) {
            if (i != 0)
            buffer.append(", ");
            buffer.append("*.");
            buffer.append(suffixes[i]);
        }
        buffer.append(")");
        return buffer.toString();
    }

    /**
    * @return Returns the spi.
    */
    public ImageReaderWriterSpi getSpi() {
        return spi;
    }
}
